package com.project.service;

import com.project.common.utils.AliUtils;
import com.project.service.base.BaseService;
import com.project.service.base.Captcha.CaptchaService;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import static com.project.common.utils.AliUtils.getTaobaoClient;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 14:12
 * @param:
 * @return:
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class MessageServiceImpl  extends BaseService implements MessageService {
    @Autowired
    CaptchaService captchaService;

    @Override
    public String sendSmsCode(String phone, String code,String smsTemplate){
        try {
            Integer expireMinute= configParameters.getAliDaYuSmsExpireMinute();
            String smsSignName=configParameters.getAliDaYuSmsSignName(); //签名
            String product= configParameters.getAliDaYuProduct(); //产品名称

            //阿里大鱼
            TaobaoClient client = getTaobaoClient();
            AlibabaAliqinFcSmsNumSendRequest req = AliUtils.getAlidayuRequest(phone,code,product,expireMinute,smsSignName,smsTemplate);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            System.out.println(rsp.getBody());
        }catch (Exception e)
        {
            e.printStackTrace();
            return "error";
        }
        //将短信验证码加入cache
//        captchaService.addCaptcha(phone,code);

        return "success";
    }

    @Override
    public String sendEmail(HttpServletRequest request){
        String mailCode="";
        String email = "";
        try {
            email = request.getParameter("email");
            mailCode = getSmsCode();
            JavaMailSenderImpl mailSender = getMailSender();
            //发送HTML邮件
            mailSender.setDefaultEncoding("UTF-8");
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
            mimeMessageHelper.setText("<font color='red'>您的验证码为"+mailCode+",此验证码仅用于UAuth认证平台,5分钟内有效。如非本人操作请忽略。</font>",true);
            mimeMessageHelper.setFrom(mailSender.getUsername());
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("UAuth邮箱验证");
            log.info(mailCode);
            // 发送邮件
            mailSender.send(mimeMessage);
        }
        catch(Exception e) {
            e.printStackTrace();
            return "error";
        }
        //将邮箱验证码加入cache
        captchaService.addCaptcha(email,mailCode);
        return "success";
    }
}
